/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import beans.AdminBean;
import java.sql.*;

/**
 *
 * @author quangphamngoc
 */
public class LoginDB {
    private static Connection con;
    private static Statement stmt;
    private static PreparedStatement pstmt;
    private static CallableStatement cstmt;
    private static ResultSet rs;
    public static AdminBean currentAdmin;
    public static int error = 0;
    //0 = no error
    //1 = SQL Error
    //2 = Driver Not Found Error
    //3 = Wrong info
    
    public static boolean connect(){
        
        try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		System.out.println("Where is your MySQL JDBC Driver?");
		e.printStackTrace();
                error = 2;
		return false;
                
	}
        // TODO code application logic here
        try {
		con = DriverManager.getConnection("jdbc:mysql://localhost:" + ConfigDB.DBPORT + "/" + ConfigDB.DBNAME,ConfigDB.DBUSER, ConfigDB.DBPASS);
 
	} catch (SQLException e) {
		System.out.println("Connection Failed! Check output console");
                //Co the do sai ten db
                error = 1;
		e.printStackTrace();
		return false;
	}
 
	if (con != null) {
		System.out.println("You made it, take control your database now!");
                return true;
	} else {
		System.out.println("Failed to make connection!");
                return false;
	}
    }
    
    public static void disconnect(){
        try {
            con.close();
        } catch (SQLException ex) {
            error = 1;
            System.out.println(ex);
            ex.printStackTrace();
        }
    }
    
     public static boolean doLogin(String username, String password){
        boolean result = false;
        if (!connect()){
            
            return false;
        }
        
        try {
            String query = "SELECT * FROM "+ ConfigDB.AdminTable +" INNER JOIN " + ConfigDB.RoleTable + " ON " + ConfigDB.AdminTable + "." + ConfigDB.RoleIDColumn + "=" + ConfigDB.RoleTable + "." + ConfigDB.RoleIDColumn + " WHERE "+ ConfigDB.AdminNameColumn +" = ? AND "+ ConfigDB.AdminPassColumn +  "= ?";
            System.out.println(query);
            pstmt = con.prepareStatement(query);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            if (rs.next()){
                currentAdmin = new AdminBean();
                currentAdmin.setAd_ID(rs.getInt("Ad_ID"));
                currentAdmin.setRole_ID(rs.getInt("Role_ID"));
                currentAdmin.setAd_Account(rs.getString("Ad_Account"));
                currentAdmin.setAd_Password(rs.getString("Ad_Password"));
                currentAdmin.setAd_Fullname(rs.getString("Ad_Fullname"));
                currentAdmin.setAd_Avatar(rs.getString("Ad_Avatar"));
                currentAdmin.setAd_Phone(rs.getString("Ad_Phone"));
                currentAdmin.setAd_Email(rs.getString("Ad_Email"));
                currentAdmin.setAd_Status(rs.getInt("Ad_Status"));
                currentAdmin.setAd_StartTime(rs.getDate("Ad_StartDate"));
                currentAdmin.setRole_Name(rs.getString("Role_Name"));
                return true;
            } else{
                error = 3;
                return false;
            }
        } catch (SQLException ex) {
            error = 1;
            System.out.println(ex);
            ex.printStackTrace();
        } finally{
            disconnect();
        }
        return result;
    }
}
